import akka.actor.ActorSystem
import akka.cluster.client.{ClusterClient, ClusterClientSettings}
import akka.util.Timeout
import com.typesafe.config.ConfigFactory

import scala.concurrent.Await

/**
  * Created by dl 
  */
object ClusterClientMain {
  def main(args: Array[String]): Unit = {

    val conf = ConfigFactory.load("client")
    val system = ActorSystem("myclient", conf) //actor system名字随意，和集群名无关

    val clusterClient = system.actorOf(ClusterClient.props(ClusterClientSettings(system)), "clusterClient")

    // 向集群某个路径下的actor发送消息，对应路径的actor需在集群端提前向集群接待员注册好服务
    clusterClient ! ClusterClient.Send("/user/masterProxy", 100, localAffinity = true)
    // 注意，不是简单的：   clusterClient ! 100

    clusterClient ! ClusterClient.Send("/user/masterProxy", "Hi", localAffinity = true)

    import akka.pattern._, scala.concurrent.duration._
    import scala.language.postfixOps

    implicit val timeout = Timeout(3 seconds)

    println(Await.result(clusterClient ? ClusterClient.Send("/user/masterProxy", 12, localAffinity = true), 5 seconds))

    println("main done")
  }
}
